//
//  HZTemaiGoodsListData.m
//  app
//
//  Created by 刘华舟 on 15/7/9.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZTemaiGoodsListData.h"

static const NSInteger kPageSize = 15;

@implementation HZTemaiGoodsListData


+ (instancetype)dataWithDictionary:(NSDictionary *)dict{
    return [[HZTemaiGoodsListData alloc] initDataWithDictionary:dict];
}
- (instancetype)initDataWithDictionary:(NSDictionary *)result{
    if (self = [super init]) {
        
        self.headerViewHeight = [UBO_SCREEN uh:kTeMaiGoodsListMainViewHeight];
        
        self.userInfo = [result dictForKey:APIKEY_mall_user_info];
        
        self.timeoffset = 0;
        //计算上图片的高度
        self.topBanners = [result arrayForKey:APIKEY_mall_banners1];
        
        if (self.topBanners.count == 1 || self.topBanners.count == 2) {
            self.headerViewHeight += [UBO_SCREEN uh:24 oh:[UBO_SCREEN fw:UB_1_3]];
        }else if (self.topBanners.count >2){
            self.headerViewHeight += [UBO_SCREEN uh:24 oh:[UBO_SCREEN fw:UB_1_2]];
        }
        //计算下图片的高度
        self.bottomBanners = [result arrayForKey:APIKEY_mall_banners2];
        
        if (self.bottomBanners.count >= 1) {
            self.headerViewHeight += [UBO_SCREEN uh:24 oh:[UBO_SCREEN fw:UB_1_3]];
        }
        
        NSMutableArray *dataArray = [NSMutableArray array];
        for (NSDictionary *dict in [result arrayForKey:APIKEY_mall_products_list]) {
            HZGoodsModel *model = [HZGoodsModel modelWithDictionary:dict];
            [dataArray addObject:model];
        }
        
        self.goodsArray = dataArray;
        if (self.goodsArray.count < kPageSize) {
            self.noMoreData = YES;
        }else{
            self.noMoreData = NO;
        }
        
        self.lastObjectIndex = self.goodsArray.count - 1;
        
        
    }
    return self;
}

-(HZGoodsModel *)fetchCellModelAtRow:(NSInteger)row{
    return [self.goodsArray objectAtIndex:row];
}

- (BOOL)addMoreDataWithArray:(NSArray *)moreDatas{
    if (moreDatas.count <= 0) {
        self.noMoreData = YES;
        return NO;
    }else{
        if (moreDatas.count < kPageSize) {
            self.noMoreData = YES;
        }
        
        NSMutableArray *totalArray = [NSMutableArray arrayWithArray:self.goodsArray];
        for (NSDictionary *dict in moreDatas) {
            HZGoodsModel *model = [HZGoodsModel modelWithDictionary:dict];
            model.modelType = HZGoodsModelTypeNormal;
            [totalArray addObject:model];
        };
        self.goodsArray = totalArray;
        
        self.lastObjectIndex = self.goodsArray.count - 1;
    }
    return YES;
}


@end
